
package com.compuvest.webstatus;

// <editor-fold defaultstate="collapsed" desc="import">
import java.io.IOException;
import java.io.PrintWriter;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.compuvest.web.SQLBean;
// </editor-fold>

public class WebStatus extends HttpServlet {

    public SQLBean sb;

// <editor-fold defaultstate="collapsed" desc="init()">

    @Override
    public void init(ServletConfig config) throws ServletException {
	super.init(config);

        Logger.getLogger(WebStatus.class.getName()).log(Level.CONFIG, "WebStatus() Init:", "");
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="processRequest()">

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");

        String id = request.getParameter("id");
        if(id == null) return;

        if("0d33c0c15a4838dde681654new".equals(id)) {
            sb = (SQLBean)request.getSession().getServletContext().getAttribute("sb");
            if(sb == null) {
                Logger.getLogger(WebStatus.class.getName()).log(Level.SEVERE, "WebStatus() processRequest", "new sb=null");
                return;
            }

            WebStat webStat = sb.loadWebStats();
            PrintWriter out = response.getWriter();
            try {
                out.print(webStat.getJSON());
            }
            finally {
                out.close();
            }
        }
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods.">

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }
    @Override
    public String getServletInfo() {
        return "WebStatus";
    }

    @Override
    public void destroy() {
        Logger.getLogger(WebStatus.class.getName()).log(Level.CONFIG, "WebStatus() Destroy:", "");
    }
// </editor-fold>

}
